import React, { useEffect } from 'react';
import AuthorizeHeader from './auth_header/authorizeHeader';
import CancelHeader from './auth_header/cancelHeader';
import AuthorizeContent from './auth_content/authorizeContent';
import CancelContent from './auth_content/cancelContent';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import * as actions from '../action/index';
import exceed from 'utils/apimap';
import utils from 'utils';
import './index.scss';
function Root(props) {
  const { getAuthInfo, getApplicationInfo, changeAuthFinishedStatus } = props;
  useEffect(() => {
    exceed
      .fetch({
        api: 'authorize.get',
        params: {
          app_id: utils.getUrlSearchParam('app_id'),
        },
      })
      .then((result) => {
        if (result) {
          changeAuthFinishedStatus({
            isBindFinished: true,
            isUnBindFinished: false,
          });
        }
        getAuthInfo(result);
      })
      .fail((err) => {
        console.log(err);
      });
    exceed
      .fetch({
        api: 'application.get',
        params: {
          app_id: utils.getUrlSearchParam('app_id'),
        },
      })
      .then((result) => {
        getApplicationInfo(result);
      })
      .fail((err) => {
        console.log(err);
      });
  }, [getAuthInfo, getApplicationInfo]);
  if (window.location.pathname === '/oauth/authorize') {
    return (
      <>
        <p className="auth_title">云效 Codeup OAuth 授权</p>
        <div className="auth_wrapper">
          <AuthorizeHeader {...props} />
          <AuthorizeContent {...props} />
        </div>
      </>
    );
  } else if (window.location.pathname === '/oauth/cancel') {
    return (
      <>
        <p className="auth_title">云效 Codeup OAuth 授权</p>
        <div className="auth_wrapper">
          <CancelHeader {...props} />
          <CancelContent {...props} />
        </div>
      </>
    );
  }
}
export default connect(
  (state) => state.auth.root,
  (dispatch) => bindActionCreators(actions, dispatch),
)(Root);
